//
//  LKBoTongFeedbackWebViewController+Navigation.m
//  iOS-BTong-ApplicationCenter
//
//  Created by maqz on 2018/4/2.
//  Copyright © 2018年 毛毛驴. All rights reserved.
//

#import "LKBoTongFeedbackWebViewController+Navigation.h"

@interface LKBoTongFeedbackBarButtonItem : UIBarButtonItem
@property (copy, nonatomic)NSString *code;
@end

@implementation LKBoTongFeedbackBarButtonItem

@end


@implementation LKBoTongFeedbackWebViewController (Navigation)

NSString *BotongApplicationCenterNavigationJSHandler = @"BotongApplicationCenterNavigationJSHandler";
static NSString const*scriptNaviType = @"naviType";
static NSString const*scriptTitles = @"titles";
static NSString const*scriptCode = @"code";
static NSString const*scriptName = @"name";
static NSString const*scriptImage = @"image";
static NSString const*scriptNotEnable = @"notEnable";


- (void)scriptNavitationMessage:(WKScriptMessage *)message {
    
    if ([message.name isEqualToString:BotongApplicationCenterNavigationJSHandler]) {
        
        NSDictionary *params = message.body[scriptParams];
        if (!params || ![params isKindOfClass:[NSDictionary class]]) {
            return;
        }
        LKBoTongFeedbackWebViewNaviType naviType = [params[scriptNaviType] integerValue];
        self.naviType = naviType;
        
        [self setupNacigationBarWithType:naviType];
        
        NSArray *titles = [self titlesArrayWithBody:message.body];
        NSMutableArray *items = [NSMutableArray array];
        for (NSDictionary *data in titles) {
            [items addObject:[self scriptBarButtonItemWithData:data naviType:naviType]];
        }
        self.navigationItem.rightBarButtonItems = items.copy;
        
    }
}

// MARK: Private method

- (UIBarButtonItem *)scriptBarButtonItemWithData:(NSDictionary *)data naviType:(LKBoTongFeedbackWebViewNaviType)type {
    NSString *imageString = data[scriptImage];
    NSString *code = data[scriptCode];
    NSString *name = data[scriptName];
    BOOL notEnable = [data[scriptNotEnable]?:@0 boolValue];
    
    UIBarButtonItem *item = nil;
    if (imageString && imageString.length) {
        if ([imageString isEqualToString:@"search"]) {
            item = [self scriptBarButtonItemWithImage:[UIImage imageNamed:@"BTNetDisk_search_h"] code:code naviType:type];
        } else if ([imageString isEqualToString:@"share"]) {
            item = [self scriptBarButtonItemWithImage:[UIImage imageNamed:@"BTCommon_share"] code:code naviType:type];
        } else if ([imageString isEqualToString:@"collected"]) {
            item = [self scriptBarButtonItemWithImage:[UIImage imageNamed:@"LKBoTongFeedback_info_collected"] code:code naviType:type];
        } else if ([imageString isEqualToString:@"notCollected"]) {
            item = [self scriptBarButtonItemWithImage:[UIImage imageNamed:@"LKBoTongFeedback_info_notCollected"] code:code naviType:type];
        } else if ([imageString isEqualToString:@"delete"]) {
             item = [self scriptBarButtonItemWithImage:[UIImage imageNamed:@"LKBoTongFeedback_navigation_delete"] code:code naviType:type];
        } else {
            item = [self scriptBarButtonItemWithTitle:name code:code naviType:type];
        }
    } else {
        item = [self scriptBarButtonItemWithTitle:name code:code naviType:type];
    }
    UIColor *tintColor = type == 0 ? [self hexColor:(0x4390E1)] : [UIColor whiteColor];
    UIColor *disabledColor = [tintColor colorWithAlphaComponent:0.4];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : disabledColor} forState:UIControlStateDisabled];
    
    item.enabled = !notEnable;
    return item;
}

- (UIBarButtonItem *)scriptBarButtonItemWithTitle:(NSString *)title code:(NSString *)code naviType:(LKBoTongFeedbackWebViewNaviType)type {
    UIColor *tintColor = type == 0 ? [self hexColor:(0x4390E1)] : [UIColor whiteColor];
    
    LKBoTongFeedbackBarButtonItem *item = [[LKBoTongFeedbackBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(scriptBarButtonItemAction:)];
    item.code = code;
    item.tintColor = tintColor;
    return item;
}

- (UIBarButtonItem *)scriptBarButtonItemWithImage:(UIImage *)image code:(NSString *)code naviType:(LKBoTongFeedbackWebViewNaviType)type {
    UIColor *tintColor = type == 0 ? [self hexColor:(0x4390E1)] : [UIColor whiteColor];
    
    LKBoTongFeedbackBarButtonItem *item = [[LKBoTongFeedbackBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(scriptBarButtonItemAction:)];
    item.code = code;
    item.tintColor = item.isEnabled ? tintColor : [tintColor colorWithAlphaComponent:0.4];
    return item;
}

- (void)setupNacigationBarWithType:(LKBoTongFeedbackWebViewNaviType)type {
    BOOL isNormal = type == 0;
    
#warning 这个方法在这里不起作用，使用下面的方式改变状态栏颜色
    [UIApplication sharedApplication].statusBarStyle = isNormal ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
    
    // 用来改变状态栏的颜色
    self.navigationController.navigationBar.barStyle = isNormal ? UIBarStyleDefault : UIBarStyleBlack;
    
    
    [self setupLeftItemsTintColor:isNormal ? [self hexColor:(0x4390E1)] : [UIColor whiteColor]];
    
    self.navigationController.navigationBar.barTintColor = isNormal ? nil : [self hexColor:(0x4390E1)];
    
    [self.navigationController.navigationBar setTitleTextAttributes: isNormal ? self.navigationBarTitleTextAttributes : @{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    // 所有页面都隐藏 navBar 的 shandowImage，所以注释一下代码
    /*
    if (@available(iOS 11, *)) {
        [self.navigationController.navigationBar setShadowImage:isNormal ? self.navigationBarShadowImage : [UIImage new]];
    } else {
        self.navigationBarLine.hidden = !isNormal;
        // 因为导航栏的 translucent 都设为 NO，所以webview 不再需要向下偏移64
//        [self updateSubViewsTopConstrint:isNormal ? 64 : 0];
    }
     */
}


- (void)setupLeftItemsTintColor:(UIColor *)color {
    for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setTintColor:color];
    }
}

// MARK: bar button item action
- (void)scriptBarButtonItemAction:(LKBoTongFeedbackBarButtonItem *)sender {
    NSDictionary *body = self.scriptMessages[BotongApplicationCenterNavigationJSHandler];
    NSArray *titles = [self titlesArrayWithBody:body];
    for (NSDictionary *data in titles) {
        NSString *name = data[scriptCode];
        if (name && [name isEqualToString:sender.code]) {
            if (!body || ![body isKindOfClass:[NSDictionary class]]) {
                return;
            }
            NSString *methodName = body[scriptCallbackMethod];
            NSString *code = data[scriptCode]?:@"";
            NSString *javaScriptString = [NSString stringWithFormat:@"%@('%@')", methodName, code];
            [self evaluateJavaScript:javaScriptString completionHandler:nil];
        }
    }
}


- (NSArray *)titlesArrayWithBody:(NSDictionary *)body {
    if (!body || ![body isKindOfClass:[NSDictionary class]]) {
        return @[];
    }
    NSDictionary *params = body[scriptParams];
    if (!params || ![params isKindOfClass:[NSDictionary class]]) {
        return @[];
    }
    
    NSArray *titles = params[scriptTitles];
    if (!titles || ![titles isKindOfClass:[NSArray class]]) {
        return @[];
    }
    
    return titles;
}

- (UIColor *)hexColor:(NSInteger)hexValue {
    CGFloat red = (CGFloat)((hexValue & 0xFF0000) >> 16) / 255.0;
    CGFloat green = (CGFloat)((hexValue & 0xFF00) >> 8) / 255.0;
    CGFloat blue = (CGFloat)((hexValue & 0xFF)) / 255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end
